package experiment.ndcg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by julianzliu on 5/7/2017.
 */
public class NormalizedDiscountedCumulativeGain {

    public static Double CalculateDCG(List<Double> rankedList){

        Double dcg_score = 0.0;

        Integer i = 1;
        for(Double score: rankedList){
            dcg_score += (  (Math.pow(2, score) - 1) / (Math.log(i+1) / Math.log(2)) );
        }

        return dcg_score;

    }


    public static Double CalculateNDCG(List<Double> bestRank, List<Double> currRank){
        if(bestRank.size() != currRank.size() ){
            System.out.println( "ERROR");
            return 0.0;
        }
        Double idcg = CalculateDCG(bestRank);
        Double dcg = CalculateDCG(currRank);

        Double ndcg = dcg / idcg;

        return ndcg;
    }



    public static void main(String[] args){
        Double[] bestRank = {3.0};//{3.0,2.0,1.0};
        Double[] currRank = {2.0}; ////{3.0,2.0,2.0,1.0,1.0,1.0};

        List<Double> bestRankList = new ArrayList<>(Arrays.asList(bestRank) );
        List<Double> currRankList = new ArrayList<>(Arrays.asList(currRank) );

        Double ndcg = CalculateNDCG(bestRankList, currRankList);

        System.out.println("[NDCG = ] " + ndcg);
    }

}
